var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
import * as echarts from '../../ec-canvas/echarts';
const apiserverUrl = require('../../config').apiserverUrl;
const AnswerExamUrl = require('../../config').AnswerExamUrl;
const GetUserExamListUrl = require('../../config').GetUserExamListUrl;
const ExamGaugeChartUrl = require('../../config').ExamGaugeChartUrl;

const app = getApp();

Page({
  data: {
    loading: true,
    userInfo: [],
    isadmin: false,
    tabs: ["知识点", "成绩单"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    scoreList: []
  },
  onShow:function(){
    this.loadGaugeChart();
  },
  loadGaugeChart:function(){
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: ExamGaugeChartUrl, data: {
          userid: this.data.userInfo.userid
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          this.ecGraphComponent = this.selectComponent('#mychart-dom-graph');
          this.ecGraphComponent.init((canvas, width, height) => {
            // 获取组件的 canvas、width、height 后的回调函数
            // 在这里初始化图表
            const chart = echarts.init(canvas, null, {
              width: width,
              height: height
            });
            this.initChart(chart, res.data.Data);
            // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
            this.chart = chart;
            // 注意这里一定要返回 chart 实例，否则会影响事件处理等
            return chart;
          });
          this.setData({ loading: false });
        }
      }
    });
  },
  onLoad: function (options) {
    this.setData({ userInfo: app.globalData.userInfo });
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    this.loadGaugeChart();
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    console.log(e.currentTarget.id);
    if (e.currentTarget.id == 1) {
      //获取用户考试成绩单
      wx.request({
        method: "post",//请求数据用get，添加数据post，修改数据put
        url: apiserverUrl,
        data: {
          method: "GET", url: GetUserExamListUrl, data: {
            userid: this.data.userInfo.userid
          }
        },
        header: {
          "Content-Type": "application/json"
        },
        success: res => {
          if (res.data.IsSuccess) {
            this.setData({ scoreList: res.data.Data });
          }
        }
      });
    }
  },
  initChart: function (chart, chartData) {
    const option = {
      backgroundColor: "#fff",
      color: ["#c1272d"],
      tooltip: {},
      xAxis: {
        show: false
      },
        
      yAxis: {
        show: false
      },
      radar: chartData.radar,
      series: chartData.series
    };
    chart.setOption(option);
  }
});